/*
 *  Copyright (C) 2010 Fabien Renaud
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.boc.botv.model;
// Generated Jun 11, 2010 10:44:35 PM by Hibernate Tools 3.2.1.GA

import com.boc.botv.common.BetInstanceStatus;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 * Représente un jeu sur un pari selon un choix défini par un utilisateur pour un montant donnée
 * @author Fabien Renaud
 */
@Entity
@Table(name = "BetInstance", catalog = "botv")
public class BetInstance implements java.io.Serializable {
  private static final SimpleDateFormat dateLongFormatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

  private Integer id;
  private BetChoice betChoice;
  private User user;
  private Bet bet;
  private double betAmount;
  private Date dateOfOperation;
  private int status;
  //Transient
  private double amount;

  /**
   * Crée un jeu par défaut sur un pari
   */
  public BetInstance() {
  }

  /**
   * Crée un jeu sur un pari
   * @param betChoice Choix de jeu
   * @param user Utilisateur qui joue
   * @param bet Pari loué
   * @param betAmount Montant misé
   * @param dateOfOperation Date de la mise
   * @param status Statut du pari
   */
  public BetInstance(BetChoice betChoice, User user, Bet bet, double betAmount, Date dateOfOperation, int status) {
    this.betChoice = betChoice;
    this.user = user;
    this.bet = bet;
    this.betAmount = betAmount;
    this.dateOfOperation = dateOfOperation;
    this.status = status;
  }

  /**
   * Obtient l'id du jeu
   * @return L'id du jeu
   */
  @Id
  @GeneratedValue(strategy = IDENTITY)
  @Column(name = "id", unique = true, nullable = false)
  public Integer getId() {
    return this.id;
  }

  /**
   * Modifie l'id du jeu
   * @param id Nouvel id du jeu
   */
  public void setId(Integer id) {
    this.id = id;
  }

  /**
   * Obtient le choix du jeu
   * @return Le choix de jeu
   */
  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "choice", nullable = false)
  public BetChoice getBetChoice() {
    return this.betChoice;
  }

  /**
   * Modifie le choix du jeu
   * @param betChoice Nouveau choix du jeu
   */
  public void setBetChoice(BetChoice betChoice) {
    this.betChoice = betChoice;
  }

  /**
   * Obtient l'utilisateur jouant
   * @return L'utilisateur jouant
   */
  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "user", nullable = false)
  public User getUser() {
    return this.user;
  }

  /**
   * Modifie l'utilisateur jouant
   * @param user L'utilisateur jouant
   */
  public void setUser(User user) {
    this.user = user;
  }

  /**
   * Obtient la pari joué
   * @return Le pari joué
   */
  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "bet", nullable = false)
  public Bet getBet() {
    return this.bet;
  }

  /**
   * Modifie le pari joué
   * @param bet Nouveau pari joué
   */
  public void setBet(Bet bet) {
    this.bet = bet;
  }

  /**
   * Obtient la somme misée
   * @return La somme misée
   */
  @Column(name = "betAmount", nullable = false, precision = 22, scale = 0)
  public double getBetAmount() {
    return this.betAmount;
  }

  /**
   * Modifie la somme misée
   * @param betAmount Nouvelle somme à miser
   */
  public void setBetAmount(double betAmount) {
    this.betAmount = betAmount;
  }

  /**
   * Obtient la date de première participation pari
   * @return La date de première participation pari
   */
  @Temporal(TemporalType.TIMESTAMP)
  @Column(name = "dateOfOperation", nullable = false, length = 19)
  public Date getDateOfOperation() {
    return this.dateOfOperation;
  }

  /**
   * Modifie la date de première participation pari
   * @param dateOfOperation Nouvelle date
   */
  public void setDateOfOperation(Date dateOfOperation) {
    this.dateOfOperation = dateOfOperation;
  }

  @Transient
  public String getDateOfOperationFormatted() {
    return dateLongFormatter.format(dateOfOperation);
  }

  public void setDateOfOperationFormatted() {
    
  }

  /**
   * Obtient le statut actuel du pari
   * @return Le statut actuel du pari
   */
  @Column(name = "status", nullable = false)
  public int getStatus() {
    return this.status;
  }

  /**
   * Modifie le statut actuel du pari
   * @param status Nouveau statut du pari
   */
  public void setStatus(int status) {
    if (status == BetInstanceStatus.playing
            || status == BetInstanceStatus.closed
            || status == BetInstanceStatus.canceled) {
      this.status = status;
    }
  }


  public void setAmount(double amount) {
    this.amount=amount;
  }


  @Transient
  public double getAmount() {
    return this.amount;
  }
  

}
